package com.cw.serviceimpl;

import com.cw.mapper.TbItemCatMapper;
import com.cw.mapper.TbItemMapper;
import com.cw.mapper.TbTypeTemplateMapper;
import com.cw.pojo.TbItem;
import com.cw.pojo.TbItemCat;
import com.cw.pojo.TbItemExample;
import com.cw.pojo.TbTypeTemplate;
import com.cw.service.ItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@com.alibaba.dubbo.config.annotation.Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private TbItemCatMapper itemCatMapper;

    @Autowired
    private TbTypeTemplateMapper tbTypeTemplateMapper;

    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Autowired
    private TbItemMapper tbItemMapper;

    @Override
    public List<TbItemCat> getItemInfo(Long id) {
        return itemCatMapper.getItemInfo(id);
    }

    @Override
    public List<TbTypeTemplate> getTemplate() {
        return tbTypeTemplateMapper.selectByExample(null);
    }

    @Override
    public void addItem(TbItemCat tc) {
        tbItemCatMapper.insertSelective(tc);
    }

    @Override
    public TbItemCat getOneItemInfo(Long id) {
        return itemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void updateItem(TbItemCat tc) {

        tbItemCatMapper.updateByPrimaryKeySelective(tc);
    }

    @Override
    @Transactional
    public String deleteItem(Long[] ids) {

        List<Long> twoIds = tbItemCatMapper.selectIds(ids);

        List<Long> threeIds = tbItemCatMapper.selectThreeIds(twoIds);

        if (threeIds.size() > 0) {

            return "请先删除子节点";

        }else {

            tbItemCatMapper.deleteInfo(twoIds);

        }

        if (twoIds.size() > 0) {

            return "请先删除子节点";

        }else {

            tbItemCatMapper.deleteByIds(ids);
        }

        tbItemCatMapper.deleteParent(ids);

        return "删除成功";
    }

    @Override
    public Long getTypeId(Long id) {

        Long tid = tbItemCatMapper.getTypeId(id);

        return tid;
    }

    @Override
    public List<TbItemCat> getAllItem() {
        return itemCatMapper.selectByExample(null);
    }

    @Override
    public List<TbItem> getItem(Long goodsId) {

        TbItemExample ite = new TbItemExample();

        TbItemExample.Criteria criteria = ite.createCriteria();

        criteria.andGoodsIdEqualTo(goodsId);

        criteria.andStatusEqualTo("1");

        criteria.andNumGreaterThan(0);

        return tbItemMapper.selectByExample(ite);
    }

    @Override
    public TbItem getItemById(Long itemId) {
        return tbItemMapper.selectByPrimaryKey(itemId);
    }
}
